<template>
  <div class="container">
    <div class="closeBtn">
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="logoWrapper">
      <span class="iconfont iconnew"></span>
    </div>
    <AuthInput
      type="text"
      tishi="用户名/手机号码"
      :rule="/^.{5,11}$/"
      errMsg="请输入正确用户名/手机号"
      @setValue="setUsername"
    />
    <AuthInput
      type="password"
      tishi="密码"
      :rule="/^\d{3,12}$/"
      errMsg="请输入正确的密码"
      @setValue="setPassword"
    />

    <AuthBtn btnText="登录" @click.native="login" />
  </div>
</template>

<script>
import AuthInput from "../components/AuthInput";
import AuthBtn from "../components/AuthBtn";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: {
    AuthInput,
    AuthBtn,
  },
  methods: {
    // 获取数据赋值到上面的data数据里
    setUsername(newValue) {
      this.username = newValue;
      // console.log(this.username);
    },
    setPassword(newValue) {
      this.password = newValue;
      // console.log(this.password);
    },
    login() {
      this.$axios({
        method: "post",
        url: "/login",
        data: {
          username: this.username,
          password: this.password,
        },
      })
        .then((res) => {
          // console.log(res);
          const { data, message } = res.data;
          if (message === "登录成功") {
            this.$toast.success("登录成功");
            // 登录成功后，获取token并把token放入本地存储中
            localStorage.setItem("token", data.token);
            localStorage.setItem("userId", data.user.id);
            this.$router.push("/profile");
          }
        })
        .catch((err) => {
          this.$toast.success("登录失败");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 21/360 * 100vw;
  .closeBtn {
    .iconicon-test {
      font-size: 27/360 * 100vw;
    }
  }
  .logoWrapper {
    text-align: center;
    .iconnew {
      font-size: 126/360 * 100vw;
      color: #d81e06;
    }
  }
}
</style>